package com.study.selenium.controller;

import com.alibaba.fastjson.JSON;
import com.study.selenium.core.R;
import com.study.selenium.entity.Emp;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/emp")
public class EmpController {
    private List<Emp> emps;
    private int nextEmpno = 11;
    @SneakyThrows
    @PostConstruct
    public void initEmps(){
        String json = new String(Files.readAllBytes(Paths.get("emps")), StandardCharsets.UTF_8);
        emps = JSON.parseArray(json, Emp.class);

    }


    /**
     * 新增
     * @param emp
     * @return
     */
    @PostMapping("/emp")
    public R<Object> save(@RequestBody Emp emp) {
        emp.setEmpno(nextEmpno);
        nextEmpno++;
        emps.add(emp);
        return R.ok(emp);
    }

    /**
     * 根据主键删除
     * @param empno
     * @return
     */
    @DeleteMapping("/emp/{empno}")
    public R<Object> removeById(@PathVariable("empno") Integer empno) {
        for (Emp emp : emps) {
            if (emp.getEmpno() == empno) {
                emp.setDeleted(true);
            }
        }
        return R.ok().setMsg("删除成功");
    }

    /**
     * 根据主键+乐观锁版本号+逻辑删除列更新
     * @param emp
     * @return
     */
    @PutMapping("/emp")
    public R<Object> updateById(@RequestBody Emp emp) {
        for (Emp e : emps) {
            if (e.getEmpno()==emp.getEmpno()) {
                BeanUtils.copyProperties(emp,e);
            }
        }
        return R.ok().setMsg("更新成功");
    }

    /**
     * 根据主键查找
     * @param empno 主键
     * @return
     */
    @GetMapping("/emp/{empno}")
    public R<Emp> getById(@PathVariable("empno") Integer empno) {
        Emp emp = emps.stream().filter(item -> item.getEmpno() == empno).collect(Collectors.toList()).get(0);
        return R.ok(emp);
    }

    @SneakyThrows
    @PostMapping("/getEmpListByPage")
    public R<List<Emp>> selectList(@RequestBody Emp param) {
        List<Emp> result = emps.stream().filter(e -> matches(e, param)).filter(item->Boolean.FALSE.equals(item.isDeleted())).collect(Collectors.toList());
        return R.ok(result);
    }
    private static boolean matches(Emp e, Emp param) {
        Field[] fields = Emp.class.getDeclaredFields();
        boolean result = true;
        for (Field field : fields) {
            field.setAccessible(true);
            Class<?> type = field.getType();
            try {
                Object input = field.get(param);
                Object eValue = field.get(e);
                if (type.equals(String.class)) {
                    String inputValue = (String) input;
                    String eValueStr = (String) eValue;
                    if (StringUtils.hasText(inputValue)) {
                        if (!eValueStr.contains(inputValue)) {
                            result =  false;
                        }
                    }
                }else{
                    if (input != null && !input.equals(eValue)) {
                        result = false;
                    }
                }
            } catch (IllegalAccessException ex) {
                throw new RuntimeException(ex);
            }
        }
        return result;
    }

}
